class Solution {
public:
    int countSubstrings(string s) {
        int n = s.size();
        vector<vector<bool>> dp(n, vector<bool>(n));
        int ret = 0;
        for (int i = n - 1; i >= 0; i--)
        {
            for (int j = i; j < n; j++)
            {
                if (s[i] == s[j])
                    dp[i][j] = i + 1 < j ? dp[i + 1][j - 1] : true;
                if (dp[i][j]) ret++;
            }
        }
        return ret;
    }
};
class Solution {
public:
    string longestPalindrome(string s) {
        int n = s.size();
        vector<vector<bool>>  dp(n, vector<bool>(n));
        int len = 1, begin = 0;
        for (int i = n - 1; i >= 0; i--)
        {
            for (int j = i; j < n; j++)
            {
                if (s[i] == s[j])
                    dp[i][j] = i + 1 < j ? dp[i + 1][j - 1] : true;
                if (dp[i][j] && j - i + 1 > len)
                    len = j - i + 1, begin = i;
            }
        }
        return s.substr(begin, len);
    }
};
class Solution {
public:
    bool checkPartitioning(string s) {
        int n = s.size();
        vector<vector<bool>> dp(n, vector<bool>(n));
        for (int i = n - 1; i >= 0; i--)
            for (int j = i; j < n; j++)
                if (s[i] == s[j]) dp[i][j] = i + 1 < j ? dp[i + 1][j - 1] : true;

        for (int i = 1; i < n - 1; i++)
            for (int j = i; j < n - 1; j++)
                if (dp[0][i - 1] && dp[i][j] && dp[j + 1][n - 1])
                    return true;

        return false;


    }
};
int n = s.size();
vector<vector<bool>>  dp(n, vector<bool>(n));
int len = 1, begin = 0;
for (int i = n - 1; i >= 0; i--)
{
    for (int j = i; j < n; j++)
    {
        if (s[i] == s[j])
            dp[i][j] = i + 1 < j ? dp[i + 1][j - 1] : true;
        if (dp[i][j] && j - i + 1 > len)
            len = j - i + 1, begin = i;
    }
}
class Solution {
public:
    int minCut(string s) {
        int n = s.size();
        vector<vector<bool>>  Ispal(n, vector<bool>(n));
        for (int i = n - 1; i >= 0; i--)
        {
            for (int j = i; j < n; j++)
            {
                if (s[i] == s[j])
                    Ispal[i][j] = i + 1 < j ? Ispal[i + 1][j - 1] : true;
            }
        }
        vector<int> dp(n, INT_MAX);
        for (int i = 0; i < n; i++)
        {
            if (Ispal[0][i]) dp[i] = 0;
            else
            {
                for (int j = 1; j < n; j++)
                {
                    if (Ispal[j][i])
                        dp[i] = min(dp[i], dp[j - 1] + 1);
                }
            }
        }
        return dp[n - 1];
    }
};